<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <img src="../../assets/image/logo_64.png" width="64" height="64" style="display: block;margin: 50px auto 8px;" />
        <h3 class="title" style="text-align: center;margin-bottom: 24px;">
          <!-- <img src="../../assets/image/logo.png" style="display: inline-block;margin-right: 5px;width: 20px;vertical-align: text-bottom;" /> -->
          <span>4万1万后台管理系统</span>
        </h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input ref="username" v-model="loginForm.username" placeholder="请输入11位手机号" name="username" maxlength="11" type="text" tabindex="1" auto-complete="on" @keyup.enter.native="handleLogin"/>
      </el-form-item>
      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" maxlength="16" placeholder="请输入6~16位密码" name="password" tabindex="2" auto-complete="on" @keyup.native="checkCapslock" @blur="capsTooltip = false" @keyup.enter.native="handleLogin"/>
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>
      <el-form-item prop="imageCode">
        <span class="svg-container">
          <svg-icon icon-class="dashboard" />
        </span>
        <el-input maxlength="6" tabindex="3" v-model="loginForm.imageCode" ref="imageCode" placeholder="请输入验证码" @keyup.enter.native="handleLogin" />
        <img :src="imageCode" style="position: absolute;right: 0;bottom: 0;width: 85px;height: 40px;cursor:pointer;" title="单击可刷新验证码" @click="getImageCode">
      </el-form-item>
      <div style="text-align: right;margin-top: 24px;">
        <el-button :loading="loading" type="primary" class="login_btn" @click.native.prevent="handleLogin" style="width: 100%;">登录</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { isvalidPhone, isvalidNumber } from '@/utils/validate'
import LangSelect from '@/components/LangSelect'
import SocialSign from './components/SocialSignin'
import { getCode } from '@/api/user'

export default {
  name: 'Login',
  components: { LangSelect, SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value) {
        if (!isvalidPhone(value)) {
          callback(new Error('手机号为11位数字，如18688889999'))
        }else{
          callback()
        }
      } else {
        callback(new Error('请输入手机号'))
      }
    }
    const validatePassword = (rule, value, callback) => {
      if(value){
        if (value.length < 6) {
          callback(new Error('请输入6~16位密码'))
        }else{
          callback()
        }
      }else{
        callback(new Error('请输入密码'))
      }
    }
    const validateYzm = (rule, value, callback) => {
      if (value) {
        if(value.length < 6){
          callback(new Error('验证码为6位数字，如666666'))
        }else{
          callback()
        }
      } else {
        callback(new Error('请输入验证码'))
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        imageCode: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        imageCode: [{ required: true, trigger: 'blur', validator: validateYzm }],
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      imageCode: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    } else if (this.loginForm.imageCode === '') {
      this.$refs.imageCode.focus()
    }
    this.getImageCode();
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        if (shiftKey && (key >= 'a' && key <= 'z') || !shiftKey && (key >= 'A' && key <= 'Z')) {
          this.capsTooltip = true
        } else {
          this.capsTooltip = false
        }
      }
      if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then((response) => {
              this.$store.dispatch('user/findMenuList',response.object,'2c91363b6c2744e5016c279271490000','2c91363b6c2744e5016c2794cd360001')
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.getImageCode();
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getImageCode(){
      const _this = this;
      getCode().then(response => {
          if(response.code == 200){
            _this.imageCode = response.object;
          }else{
              this.$message.error(response.message);
          }
      })
    },
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  }
}
</script>
<style>
  .el-button--primary.el-button--medium.login_btn{
    /* background: #0067b8; */
    /* width: 108px; */
    border-radius: 4px;
  }
</style>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

.login-container{
  background: url('../../assets/image/login_bg.jpg') no-repeat;
  background-size: 100% 100%;
}

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {
//      color: $cursor;
//   }
// }

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      // color: $light_gray;
      height: 47px;
      // caret-color: $cursor;
      vertical-align: middle;
      text-align: center;

      &:-webkit-autofill {
        // box-shadow: 0 0 0px 1000px $bg inset !important;
        // -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border-bottom: 1px solid rgba(0,0,0,.6);
    color: #454545;
    margin-bottom: 0px;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 460px;
    max-width: 100%;
    padding: 0px 50px 50px;
    margin: 240px auto 0;
    background: #f5f5f5;
    // background: #fff;
    overflow: hidden;
    -webkit-box-shadow: 0 2px 6px rgba(0,0,0,.2);
    -moz-box-shadow: 0 2px 6px rgba(0,0,0,.2);
    box-shadow: 0 2px 6px rgba(0,0,0,.2);
    border-radius: 3px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 21px;
      color: #666;
      margin: 0px auto 20px auto;
      text-align: left;
    }

    .set-language {
      // color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
